﻿using Xejen.Commands.Models;
using Xejen.Communication.Endpoints;
using Xejen.Communication.Socket.Communicator;
using Microsoft.Extensions.Configuration;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.Json;
using System.Threading.Tasks;

namespace Xejen.Communication.Socket.Client.EventTest
{
    public class Service
    {
        private readonly ISocketCommunicator _communicator;
        private readonly IConfiguration _configuration;
        //public Service(ISocketCommunicator communicator, IConfiguration configuration)
        //{
        //    _communicator = communicator;
        //    _configuration = configuration;
        //}

        public Service(IEnumerable<ISocketCommunicator> communicators, IConfiguration configuration)
        {
            _communicator = communicators.First(q => q.CurrentConnection.ServerAddress == "127.0.0.1");
            _configuration = configuration;
        }

        public async Task ExecuteAsync()
        {
            //Addresses addresses = new Addresses();
            //_configuration.Bind(addresses);
            //var result1 = await _communicator.RequestAsync(SocketAddressWrapper.Create(addresses.GetWeatherForecast));

            //var result1 = await _communicator.SendAsync("SocketAsyncEventArgs方式：fdsa哈根达斯魂牵梦萦人斯柯达  魂牵梦萦为Q@$(*$1硒鼓");
            //Console.WriteLine(result1.ToString());

            var jsonBytes = Encoding.UTF8.GetBytes(GetUserLoginInfoData().ToJson());
            var result2 = await _communicator.SendAsync(jsonBytes);
            Console.WriteLine(result2);
        }

        /// <summary>
        /// 取UserLoginInfo的请求参数
        /// </summary>
        /// <returns></returns>
        private UserLoginInfoRequest GetUserLoginInfoData()
        {
            UserLoginInfoRequest model = new UserLoginInfoRequest();
            model.HostName = "LJSERVER";
            model.ResourceNo = "DL05-GX010-01-026";
            model.MethodName = "UserLogin_info";
            model.MethodId = Guid.NewGuid().ToString();
            model.SessionId = string.Empty;
            model.UserId = "TEST001";
            model.Password = "123456";

            return model;
        }
    }
}
